package com.hh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hh.entity.IndexImg;
import com.hh.service.IndexImgService;
import com.hh.vo.IndexImgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 辉辉 2022/8/18 15:28
 */
@Api(tags = "轮播图管理")
@RestController
@RequestMapping("/admin/indexImg")
public class IndexImgController {
    @Resource
    private IndexImgService indexImgService;

    @GetMapping("/page")
    @ApiOperation("分页查询轮播图")
    @PreAuthorize("hasAuthority('admin:indexImg:page')")
    public ResponseEntity<IPage<IndexImg>> indexImgPage(Page<IndexImg> page, IndexImg indexImg) {
        IPage<IndexImg> indexImgIPage = indexImgService.findIndexImgPage(page, indexImg);
        return ResponseEntity.ok(indexImgIPage);
    }

    @PostMapping
    @ApiOperation("新增轮播图")
    @PreAuthorize("hasAuthority('admin:indexImg:save')")
    public ResponseEntity<Void> addIndexImg(@RequestBody IndexImg indexImg) {
        indexImgService.save(indexImg);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/info/{id}")
    @ApiOperation("根据id查询一个轮播图")
    @PreAuthorize("hasAuthority('admin:indexImg:info')")
    public ResponseEntity<IndexImg> getIndexImgInfo(@PathVariable("id") Long id) {
        IndexImg indexImg = indexImgService.findIndexImgById(id);
        return ResponseEntity.ok(indexImg);
    }

    // -------下面是前台代码了

    /**
     * 因为微信小程序的性能比较差 基本知识 前台代码的大小必须要小于4MB
     * 性能比较差 http请求和相应的时候 处理的数据包不能过大 专门封装对象
     * @return
     */
    @GetMapping("/indexImgs")
    @ApiOperation("前台轮播图接口")
    public ResponseEntity<List<IndexImgVo>> loadFrontIndexImg() {
        List<IndexImgVo> indexImgVos = indexImgService.findFrontIndexImg();
        return ResponseEntity.ok(indexImgVos);
    }

    // -------下面是提供远程调用的接口
}
